package com.lanou.musicproject.adapter;

import android.app.ActivityManager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanou.musicproject.R;
import com.lanou.musicproject.activity.MainActivity;
import com.lanou.musicproject.activity.MusicPlayActivity;
import com.lanou.musicproject.bean.MyMusicList;
import com.lanou.musicproject.bean.RankContent;
import com.lanou.musicproject.bean.RecommHotSong;
import com.lanou.musicproject.suspendservice.FxService;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 王世美 on 15/10/26.
 * 推荐页-热门歌曲推荐
 */
public class RecommHotSongAdapter extends BaseAdapter {
    // 定义实体类数组 获取图片(pic_small),歌名(title),歌手(author),推荐理由(recommend_reason)
    private List<RecommHotSong.ContentEntity.SongListEntity> data;
    private boolean isService = false;
    // 上下文
    private Context context;

    // 有参构造方法
    public RecommHotSongAdapter(Context c, List<RecommHotSong.ContentEntity.SongListEntity> data) {
        this.data = data;
        context = c;
    }

    //获得行布局个数
    @Override
    public int getCount() {
        if (data != null && data.size() > 0)
            return data.size();
        else
            return 0;
    }

    // 获得行布局的坐标
    @Override
    public RecommHotSong.ContentEntity.SongListEntity getItem(int position) {
        if (data != null && data.size() > 0)
            return data.get(position);
        else
            return null;
    }

    // 获得行布局Id
    @Override
    public long getItemId(int position) {
        return position;
    }

    // 获得行布局内容
    @Override
    public View getView(final int position,
                        View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).
                    inflate(R.layout.recomm_listview_hotsong_item, null);
            holder = new ViewHolder(convertView);
            holder.recomm_hotItemView = (LinearLayout) convertView.findViewById(R.id.recomm_hotItemView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        RecommHotSong.ContentEntity.SongListEntity song = getItem(position);
        if (song != null) {
            Picasso.with(context).load(song.getPic_big()).into(holder.hotSongIV);
            holder.songTV.setText(song.getTitle());
            holder.authorTV.setText(song.getAuthor());
            holder.reasonTV.setText(song.getRecommend_reason());
            holder.recomm_hotItemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ArrayList<MyMusicList> lists = new ArrayList<>();
                    for (RecommHotSong.ContentEntity.SongListEntity song :data){
                        MyMusicList musicList = new MyMusicList();
                        musicList.setName(song.getTitle());
                        musicList.setSinger(song.getAuthor());
                        musicList.setSongId(song.getSong_id());
                        musicList.setPlayer_singer_image(song.getPic_big());
                        musicList.setFrom("网络");
                        lists.add(musicList);
                    }
                    MainActivity.mini_play(lists, position);
                    FxService.mini_play(lists,position);
                    MusicPlayActivity.playIv(lists, position);

                }

                });
        }
        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'recomm_listview_hotsong_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    // 初始化组件
    static class ViewHolder {
        @Bind(R.id.hotSongIV)
        ImageView hotSongIV;
        @Bind(R.id.songTV)
        TextView songTV;
        @Bind(R.id.authorTV)
        TextView authorTV;
        @Bind(R.id.reasonTV)
        TextView reasonTV;
        private LinearLayout recomm_hotItemView;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
    public boolean IsService(Context mContext ,String serviceName){
        ActivityManager myAM = (ActivityManager) mContext
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> myList = myAM.getRunningServices(40);
        if (myList.size() <= 0) {
            return false;
        }
        for (int i = 0; i < myList.size(); i++) {
            String mName = myList.get(i).service.getClassName().toString();
            if (mName.equals("com.lanou.musicproject.suspendservice.FxService")) {
                isService = true;
                break;
            }
        }
        return isService;

    }

}
